package com.jingdong.api.model;

/**
 * API配置类
 *
 * @author 林生
 * @version 1.0.0
 */
public class ApiConfig {
    
    /**
     * 应用密钥
     */
    private String appKey;
    
    /**
     * 应用密码
     */
    private String appSecret;
    
    /**
     * 商户号
     */
    private String merchantNo;
    
    /**
     * 是否沙箱模式
     */
    private boolean sandboxMode;
    
    /**
     * 超时时间（秒）
     */
    private int timeout;
    
    /**
     * 用户代理
     */
    private String userAgent;
    
    /**
     * 是否启用日志
     */
    private boolean enableLog;
    
    /**
     * 构造函数
     */
    public ApiConfig() {
        this.sandboxMode = false;
        this.timeout = 30;
        this.userAgent = "JingdongApiSDK-Java/1.0.0";
        this.enableLog = true;
    }
    
    /**
     * Builder模式构建器
     */
    public static class Builder {
        private ApiConfig config = new ApiConfig();
        
        public Builder appKey(String appKey) {
            config.appKey = appKey;
            return this;
        }
        
        public Builder appSecret(String appSecret) {
            config.appSecret = appSecret;
            return this;
        }
        
        public Builder merchantNo(String merchantNo) {
            config.merchantNo = merchantNo;
            return this;
        }
        
        public Builder sandboxMode(boolean sandboxMode) {
            config.sandboxMode = sandboxMode;
            return this;
        }
        
        public Builder timeout(int timeout) {
            config.timeout = timeout;
            return this;
        }
        
        public Builder userAgent(String userAgent) {
            config.userAgent = userAgent;
            return this;
        }
        
        public Builder enableLog(boolean enableLog) {
            config.enableLog = enableLog;
            return this;
        }
        
        public ApiConfig build() {
            config.validate();
            return config;
        }
    }
    
    public static Builder builder() {
        return new Builder();
    }
    
    /**
     * 验证配置
     * 
     * @throws IllegalArgumentException 配置错误时抛出
     */
    public void validate() {
        if (appKey == null || appKey.trim().isEmpty()) {
            throw new IllegalArgumentException("appKey 不能为空");
        }
        
        if (appSecret == null || appSecret.trim().isEmpty()) {
            throw new IllegalArgumentException("appSecret 不能为空");
        }
        
        if (merchantNo == null || merchantNo.trim().isEmpty()) {
            throw new IllegalArgumentException("merchantNo 不能为空");
        }
        
        if (timeout <= 0) {
            throw new IllegalArgumentException("timeout 必须大于0");
        }
    }
    
    // Getters and Setters
    public String getAppKey() {
        return appKey;
    }
    
    public void setAppKey(String appKey) {
        this.appKey = appKey;
    }
    
    public String getAppSecret() {
        return appSecret;
    }
    
    public void setAppSecret(String appSecret) {
        this.appSecret = appSecret;
    }
    
    public String getMerchantNo() {
        return merchantNo;
    }
    
    public void setMerchantNo(String merchantNo) {
        this.merchantNo = merchantNo;
    }
    
    public boolean isSandboxMode() {
        return sandboxMode;
    }
    
    public void setSandboxMode(boolean sandboxMode) {
        this.sandboxMode = sandboxMode;
    }
    
    public int getTimeout() {
        return timeout;
    }
    
    public void setTimeout(int timeout) {
        this.timeout = timeout;
    }
    
    public String getUserAgent() {
        return userAgent;
    }
    
    public void setUserAgent(String userAgent) {
        this.userAgent = userAgent;
    }
    
    public boolean isEnableLog() {
        return enableLog;
    }
    
    public void setEnableLog(boolean enableLog) {
        this.enableLog = enableLog;
    }
}
